/*
 * @Descripttion: 函数
 * @Author: ziwei
 * @Date: 2022-07-03 15:12:27
 * @LastEditors: ziwei
 * @LastEditTime: 2022-07-03 19:08:33
 */
//函数定义
function hello(name:string):void{  
    console.log('hello',name);
}
hello('xx')
type GetName = (firstName:string,lastName:string)=>string;
let getName:GetName = function(firstName:string,lastName:string):string{
    return firstName+lastName
}

function print(name:string,age?:number):void{
    console.log(name,age);  
}
print('zhufeng',11)
function ajax(url:string,method:string='GET'){
    console.log(url,method);
}
ajax('/')
function sum(...numbers:number[]){
    return numbers.reduce((val,item)=>val+item,0)
}
console.log(sum(1,2,3));

//函数的重载：同样一个函数传的方式不一样
let obj:any ={}

//如果传的val是一个字符串就赋给obj.name,数字赋给obj.age
function attr(val:string):void
function attr(val:number):void
function attr(val:any):void{
    if(typeof val == 'string'){
        obj.name = val
    }else if(typeof val === 'number'){
        obj.age = val
    }
}
attr('xx')
attr(10)
//attr(true)

// function attr2(val:string|number):void{
//     if(typeof val === 'string'){
//         obj.name = val
//     }else if(typeof val === 'number'){
//         obj.age = val
//     }
// }
// attr2('x')
// attr2(1)
function add(a:string,b:string):void
function add(a:number,b:number):void
function add(a:string|number,b:string|number):void{
}
add('a','b')
add(1,2)
